//
// Created by 刘禹晨 on 16/7/21.
// Copyright (c) 2016 yuchenliu. All rights reserved.
//

#import "LeftMenuCell.h"
#import "SDAutoLayout/SDAutoLayout.h"
#import "Mode.h"

@interface LeftMenuCell()
{
    UILabel *menuLabel;
    UILabel *detailLabel;
    UISwitch *modeSwitch;
    Mode *mode;
}
@end

@implementation LeftMenuCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
        //[self setupAutoHeightWithBottomView:modeSwitch bottomMargin:5];
    }
    return self;
}


- (void)setup{
    [self initMenuLabel];
    [self initdetailLabel];
    [self initSwitch];
}

- (void)initMenuLabel
{
    menuLabel = [[UILabel alloc]init];
    [menuLabel setText:@"菜单菜单"];
    menuLabel.font = [UIFont systemFontOfSize:17.0f];
    CGFloat height =  [menuLabel.text sizeWithAttributes:@{NSFontAttributeName :menuLabel.font}].height;
    CGFloat width =  [menuLabel.text sizeWithAttributes:@{NSFontAttributeName : menuLabel.font}].width;
    [self.contentView addSubview:menuLabel];
    menuLabel.sd_layout.heightIs(height).widthIs(width).leftSpaceToView(self.contentView,12).centerYEqualToView(self.contentView);
}

- (void)initdetailLabel
{
    detailLabel = [[UILabel alloc]init];
    detailLabel.text = @"255.255.255.255";
    detailLabel.font = [UIFont systemFontOfSize:15.0f];
    detailLabel.textColor = [UIColor grayColor];
    CGFloat height =  [detailLabel.text sizeWithAttributes:@{NSFontAttributeName : detailLabel.font}].height;
    CGFloat width =  [detailLabel.text sizeWithAttributes:@{NSFontAttributeName : detailLabel.font}].width;
    [self.contentView addSubview:detailLabel];
    detailLabel.sd_layout.heightIs(height).widthIs(width).leftSpaceToView(menuLabel,5).centerYEqualToView(self.contentView);
}

-(void)initSwitch
{
    modeSwitch = [[UISwitch alloc]init];
    [modeSwitch setOn:NO];
    [self.contentView addSubview:modeSwitch];
    modeSwitch.sd_layout.heightIs(31).rightSpaceToView(self.contentView,12).centerYEqualToView(self.contentView);
    [modeSwitch addTarget:self action:@selector(changeMode:) forControlEvents:UIControlEventValueChanged];
}

- (void)setMenuModel:(MenuModel *)model
{
    _menuModel = model;
    menuLabel.text = _menuModel.menuTitle;
    if([_menuModel.hasSwitch isEqualToString:@"NO"])
    {
        detailLabel.text = _menuModel.menuDetail;
        detailLabel.hidden = NO;
        modeSwitch.hidden = YES;
    }else{
        detailLabel.hidden = YES;
        modeSwitch.hidden = NO;
    }
}

- (void)changeMode:(id)sender
{
    if(modeSwitch.isOn == YES)
    {
        mode = [Mode instance];
        mode.mode = @"WiFi";
    }else{
        mode = [Mode instance];
        mode.mode = @"BLE";
    }
}

@end